<?php
/**
 * Users Controller
 *
 * PHP versions 4 and 5
 *
 * Infectamic: Free online role playing game, easily playable by browser. (http://infectamic.goldzoneweb.info)
 * Copyright 2011, Loic Chardonnet
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2011, Loic Chardonnet
 * @link          http://infectamic.goldzoneweb.info Infectamic
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class UsersController extends AppController {

    var $name = 'Users';

    function beforeFilter() {
        $this->Auth->allow('register');
    }

    function register() {
        /**
         * If posted data, treat them to register the user.
         */
        if (empty ($this->data) === false) {
            if ($this->data['User']['password'] === $this->Auth->password($this->data['User']['password_confirm'])) {
                // Removing the confirm field.
                unset ($this->data['User']['password_confirm']);

                // Initialisation of all the non-asked fields.
                $this->data['User'] = array_merge($this->data['User'], $this->User->getInitilaData());

                $this->User->create();
                if ($this->User->save($this->data)) {
                    $this->Session->setFlash(__('You have registered.', true));
                    $this->redirect(array ('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The registration could not be done. Please, try again.', true));
                    unset ($this->data['User']['password']);
                }
            } else {
                $this->Session->setFlash(__('The two passords are different. Please, try again.', true));
                unset (
                    $this->data['User']['password'],
                    $this->data['User']['password_confirm']
                );
            }
        }

        /**
         *
         */
        $designs = $this->User->Design->find('list');
        $teams = $this->User->Team->find('list');
        $naps = $this->User->Nap->find('list');
        $this->set(compact('designs', 'teams', 'naps'));
    }

    function login() {
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }

    /**
     * Auto generated admin functions.
     */

    function vogon_index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    function vogon_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function vogon_add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        $designs = $this->User->Design->find('list');
        $teams = $this->User->Team->find('list');
        $naps = $this->User->Nap->find('list');
        $groups = $this->User->Group->find('list');
        $this->set(compact('designs', 'teams', 'naps', 'groups'));
    }

    function vogon_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $designs = $this->User->Design->find('list');
        $teams = $this->User->Team->find('list');
        $naps = $this->User->Nap->find('list');
        $groups = $this->User->Group->find('list');
        $this->set(compact('designs', 'teams', 'naps', 'groups'));
    }

    function vogon_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for user', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Session->setFlash(__('User was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
}
?>